home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / c / icu-1.3.1 / icu-bin / include / decimfmt.h < prev    next >
C/C++ Source or Header  |  2000-02-23  |  38KB  |  987 lines

  1. /*
  2. ********************************************************************************
  3. *                                                                              *
  4. * COPYRIGHT:                                                                   *
  5. *   (C) Copyright Taligent, Inc.,  1997                                        *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1999      *
  7. *   Copyright (C) 1999 Alan Liu and others. All rights reserved.               *
  8. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  9. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  10. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  11. *                                                                              *
  12. ********************************************************************************
  13. *
  14. * File DECIMFMT.H
  15. *
  16. * Modification History:
  17. *
  18. *   Date        Name        Description
  19. *   02/19/97    aliu        Converted from java.
  20. *   03/20/97    clhuang     Updated per C++ implementation.
  21. *   04/03/97    aliu        Rewrote parsing and formatting completely, and
  22. *                           cleaned up and debugged.  Actually works now.
  23. *   04/17/97    aliu        Changed DigitCount to int per code review.
  24. *   07/10/97    helena      Made ParsePosition a class and get rid of the function
  25. *                           hiding problems.
  26. *   09/09/97    aliu        Ported over support for exponential formats.
  27. *    07/20/98    stephen        Changed documentation
  28. ********************************************************************************
  29. */
  30.  
  31. #ifndef DECIMFMT_H
  32. #define DECIMFMT_H
  33.  
  34. #include "utypes.h"
  35. #include "numfmt.h"
  36. #include "locid.h"
  37.  
  38. class DecimalFormatSymbols;
  39. class DigitList;
  40.  
  41. /**
  42.  * Concrete class for formatting decimal numbers, allowing a variety
  43.  * of parameters, and localization to Western, Arabic, or Indic numbers.
  44.  * <P>
  45.  * Normally, you get the proper NumberFormat for a specific locale
  46.  * (including the default locale) using the NumberFormat factory methods,
  47.  * rather than constructing a DecimalNumberFormat directly.
  48.  * <P>
  49.  * Either the prefixes or the suffixes must be different for the parse
  50.  * to distinguish positive from negative.  Parsing will be unreliable
  51.  * if the digits, thousands or decimal separators are the same, or if
  52.  * any of them occur in the prefixes or suffixes.
  53.  * <P>
  54.  * [Special cases:] 
  55.  * <P>
  56.  * NaN is formatted as a single character, typically \\uFFFD.
  57.  * <P>
  58.  * +/-Infinity is formatted as a single character, typically \\u221E,
  59.  * plus the positive and negative pre/suffixes.
  60.  * <P>
  61.  * Note: this class is designed for common users; for very large or small
  62.  * numbers, use a format that can express exponential values.
  63.  * <P>
  64.  * [Example:] 
  65.  * <pre>
  66.  * .    // normally we would have a GUI with a menu for this
  67.  * .    int32_t locCount;
  68.  * .    const Locale* locales = NumberFormat::getAvailableLocales(locCount);
  69.  * .    if (locCount > 12) locCount = 12;  //limit output
  70.  * .
  71.  * .    double myNumber = -1234.56;
  72.  * .    UErrorCode success = U_ZERO_ERROR;
  73.  * .    NumberFormat* form; //= NumberFormat::createInstance(success);
  74.  * .
  75.  * .    // just for fun, we print out a number with the locale number, currency
  76.  * .    // and percent format for each locale we can.
  77.  * .    UnicodeString countryName;
  78.  * .    UnicodeString displayName;
  79.  * .    UnicodeString str;
  80.  * .    UnicodeString pattern;
  81.  * .    Formattable fmtable;
  82.  * .    for (int32_t j = 0; j < 3; ++j) {
  83.  * .        cout << endl << "FORMAT " << j << endl;
  84.  * .        for (int32_t i = 0; i < locCount; ++i) {
  85.  * .            if (locales[i].getCountry(countryName).size() == 0) {
  86.  * .                // skip language-only
  87.  * .                continue;
  88.  * .            }
  89.  * .            switch (j) {
  90.  * .            default:
  91.  * .                form = NumberFormat::createInstance(locales[i], success ); break;
  92.  * .            case 1:
  93.  * .                form = NumberFormat::createCurrencyInstance(locales[i], success ); break;
  94.  * .            case 0:
  95.  * .                form = NumberFormat::createPercentInstance(locales[i], success ); break;
  96.  * .            }
  97.  * .            if (form) {
  98.  * .                str.remove();
  99.  * .                pattern = ((DecimalFormat*)form)->toPattern(pattern);
  100.  * .                cout << locales[i].getDisplayName(displayName) << ": " << pattern;
  101.  * .                cout << "  ->  " << form->format(myNumber,str) << endl;
  102.  * .                form->parse(form->format(myNumber,str), fmtable, success);
  103.  * .                //cout << "   parsed: " << fmtable << endl;
  104.  * .                delete form;  
  105.  * .            }
  106.  * .        }
  107.  * .    }
  108.  * </pre>
  109.  * [The following shows the structure of the pattern.] 
  110.  * <pre>
  111.  * .    pattern    := subpattern{;subpattern}
  112.  * .    subpattern := {prefix}integer{.fraction}{suffix}
  113.  * .    
  114.  * .    prefix     := '\\u0000'..'\\uFFFD' - specialCharacters
  115.  * .    suffix     := '\\u0000'..'\\uFFFD' - specialCharacters
  116.  * .    integer    := '#'* '0'* '0'
  117.  * .    fraction   := '0'* '#'*
  118.  *    
  119.  *  Notation:
  120.  * .    X*       0 or more instances of X
  121.  * .    (X | Y)  either X or Y.
  122.  * .    X..Y     any character from X up to Y, inclusive.
  123.  * .    S - T    characters in S, except those in T
  124.  * </pre>
  125.  * The first subpattern is for positive numbers. The second (optional)
  126.  * subpattern is used for negative numbers. (In both cases, ',' can
  127.  * occur inside the integer portion--it is just too messy to indicate
  128.  * in BNF.)  For the second subpattern, only the PREFIX and SUFFIX are
  129.  * noted; other attributes are taken only from the first subpattern.
  130.  * <P>
  131.  * Here are the special characters used in the parts of the
  132.  * subpattern, with notes on their usage.
  133.  * <pre>
  134.  * .    Symbol   Meaning
  135.  * .      0      a digit, showing up a zero if it is zero
  136.  * .      #      a digit, supressed if zero
  137.  * .      .      placeholder for decimal separator
  138.  * .      ,      placeholder for grouping separator.
  139.  * .      E      separates mantissa and exponent for exponential formats.
  140.  * .      ;      separates formats.
  141.  * .      -      default negative prefix.
  142.  * .      %      multiply by 100 and show as percentage
  143.  * .      \u2030 multiply by 1000 and show as per mille
  144.  * .      \u00A4 currency sign; replaced by currency symbol; if
  145.  * .             doubled, replaced by international currency symbol.
  146.  * .             If present in a pattern, the monetary decimal separator
  147.  * .             is used instead of the decimal separator.
  148.  * .      X      any other characters can be used in the prefix or suffix
  149.  * .      '      used to quote special characters in a prefix or suffix.
  150.  * </pre>
  151.  * [Notes] 
  152.  * <P>
  153.  * If there is no explicit negative subpattern, - is prefixed to the
  154.  * positive form. That is, "0.00" alone is equivalent to "0.00;-0.00".
  155.  * <P>
  156.  * Illegal formats, such as "#.#.#" in the same format, will cause a
  157.  * failing UErrorCode to be returned. 
  158.  * <P>
  159.  * The grouping separator is commonly used for thousands, but in some
  160.  * countries for ten-thousands. The interval is a constant number of
  161.  * digits between the grouping characters, such as 100,000,000 or 1,0000,0000.
  162.  * If you supply a pattern with multiple grouping characters, the interval
  163.  * between the last one and the end of the integer is the one that is
  164.  * used. So "#,##,###,####" == "######,####" == "##,####,####".
  165.  * <P>
  166.  * This class only handles localized digits where the 10 digits are
  167.  * contiguous in Unicode, from 0 to 9. Other digits sets (such as
  168.  * superscripts) would need a different subclass.
  169.  */
  170. class U_I18N_API DecimalFormat: public NumberFormat {
  171. public:
  172.     enum ERoundingMode {
  173.         kRoundCeiling,
  174.         kRoundFloor,
  175.         kRoundDown,
  176.         kRoundUp,
  177.         kRoundHalfEven,
  178.         kRoundHalfDown,
  179.         kRoundHalfUp
  180.         // We don't support ROUND_UNNECESSARY
  181.     };
  182.  
  183.     enum EPadPosition {
  184.         kPadBeforePrefix,
  185.         kPadAfterPrefix,
  186.         kPadBeforeSuffix,
  187.         kPadAfterSuffix
  188.     };
  189.  
  190.     /**
  191.      * Create a DecimalFormat using the default pattern and symbols
  192.      * for the default locale. This is a convenient way to obtain a
  193.      * DecimalFormat when internationalization is not the main concern.
  194.      * <P>
  195.      * To obtain standard formats for a given locale, use the factory methods
  196.      * on NumberFormat such as getNumberInstance. These factories will
  197.      * return the most appropriate sub-class of NumberFormat for a given
  198.      * locale.
  199.      * @param status    Output param set to success/failure code. If the
  200.      *                  pattern is invalid this will be set to a failure code.
  201.      */
  202.     DecimalFormat(UErrorCode& status);
  203.  
  204.     /**
  205.      * Create a DecimalFormat from the given pattern and the symbols
  206.      * for the default locale. This is a convenient way to obtain a
  207.      * DecimalFormat when internationalization is not the main concern.
  208.      * <P>
  209.      * To obtain standard formats for a given locale, use the factory methods
  210.      * on NumberFormat such as getNumberInstance. These factories will
  211.      * return the most appropriate sub-class of NumberFormat for a given
  212.      * locale.
  213.      * @param pattern   A non-localized pattern string.
  214.      * @param status    Output param set to success/failure code. If the
  215.      *                  pattern is invalid this will be set to a failure code.
  216.      */
  217.     DecimalFormat(const UnicodeString& pattern,
  218.                   UErrorCode& status);
  219.  
  220.     /**
  221.      * Create a DecimalFormat from the given pattern and symbols.
  222.      * Use this constructor when you need to completely customize the
  223.      * behavior of the format.
  224.      * <P>
  225.      * To obtain standard formats for a given
  226.      * locale, use the factory methods on NumberFormat such as
  227.      * getInstance or getCurrencyInstance. If you need only minor adjustments
  228.      * to a standard format, you can modify the format returned by
  229.      * a NumberFormat factory method.
  230.      *
  231.      * @param pattern           a non-localized pattern string
  232.      * @param symbolsToAdopt    the set of symbols to be used.  The caller should not
  233.      *                          delete this object after making this call.
  234.      * @param status            Output param set to success/failure code. If the
  235.      *                          pattern is invalid this will be set to a failure code.
  236.      */
  237.     DecimalFormat(  const UnicodeString& pattern,
  238.                     DecimalFormatSymbols* symbolsToAdopt,
  239.                     UErrorCode& status);
  240.  
  241.     /**
  242.      * Create a DecimalFormat from the given pattern and symbols.
  243.      * Use this constructor when you need to completely customize the
  244.      * behavior of the format.
  245.      * <P>
  246.      * To obtain standard formats for a given
  247.      * locale, use the factory methods on NumberFormat such as
  248.      * getInstance or getCurrencyInstance. If you need only minor adjustments
  249.      * to a standard format, you can modify the format returned by
  250.      * a NumberFormat factory method.
  251.      *
  252.      * @param pattern           a non-localized pattern string
  253.      * @param symbols   the set of symbols to be used
  254.      * @param status            Output param set to success/failure code. If the
  255.      *                          pattern is invalid this will be set to a failure code.
  256.      */
  257.     DecimalFormat(  const UnicodeString& pattern,
  258.                     const DecimalFormatSymbols& symbols,
  259.                     UErrorCode& status);
  260.  
  261.     /**
  262.      * Copy constructor.
  263.      */
  264.     DecimalFormat(const DecimalFormat& source);
  265.  
  266.     /**
  267.      * Assignment operator.
  268.      */
  269.     DecimalFormat& operator=(const DecimalFormat& rhs);
  270.  
  271.     /**
  272.      * Destructor.
  273.      */
  274.     virtual ~DecimalFormat();
  275.  
  276.     /**
  277.      * Clone this Format object polymorphically. The caller owns the
  278.      * result and should delete it when done.
  279.      */
  280.     virtual Format* clone(void) const;
  281.  
  282.     /**
  283.      * Return true if the given Format objects are semantically equal.
  284.      * Objects of different subclasses are considered unequal.
  285.      */
  286.     virtual bool_t operator==(const Format& other) const;
  287.  
  288.    /**
  289.     * Format a double or long number using base-10 representation.
  290.     *
  291.     * @param number     The value to be formatted.
  292.     * @param toAppendTo The string to append the formatted string to.
  293.     *                   This is an output parameter.
  294.     * @param pos        On input: an alignment field, if desired.
  295.     *                   On output: the offsets of the alignment field.
  296.     * @return           A reference to 'toAppendTo'.
  297.     */
  298.     virtual UnicodeString& format(double number,
  299.                                   UnicodeString& toAppendTo,
  300.                                   FieldPosition& pos) const;
  301.     virtual UnicodeString& format(int32_t number,
  302.                                   UnicodeString& toAppendTo,
  303.                                   FieldPosition& pos) const;
  304.     virtual UnicodeString& format(const Formattable& obj,
  305.                                   UnicodeString& toAppendTo,
  306.                                   FieldPosition& pos,
  307.                                   UErrorCode& status) const;
  308.  
  309.     /**
  310.      * Redeclared NumberFormat method.
  311.      */
  312.     UnicodeString& format(const Formattable& obj,
  313.                           UnicodeString& result,
  314.                           UErrorCode& status) const;
  315.  
  316.     /**
  317.      * Redeclared NumberFormat method.
  318.      */
  319.     UnicodeString& format(double number,
  320.                           UnicodeString& output) const;
  321.  
  322.     /**
  323.      * Redeclared NumberFormat method.
  324.      */
  325.     UnicodeString& format(int32_t number,
  326.                           UnicodeString& output) const;
  327.  
  328.    /**
  329.     * Parse the given string using this object's choices. The method
  330.     * does string comparisons to try to find an optimal match.
  331.     * If no object can be parsed, index is unchanged, and NULL is
  332.     * returned.
  333.     *
  334.     * @param text           The text to be parsed.
  335.     * @param result         Formattable to be set to the parse result.
  336.     *                       If parse fails, return contents are undefined.
  337.     * @param parsePosition  The position to start parsing at on input.
  338.     *                       On output, moved to after the last successfully
  339.     *                       parse character. On parse failure, does not change.
  340.     */
  341.     virtual void parse(const UnicodeString& text,
  342.                        Formattable& result,
  343.                        ParsePosition& parsePosition) const;
  344.  
  345.     // Declare here again to get rid of function hiding problems.
  346.     virtual void parse(const UnicodeString& text, 
  347.                        Formattable& result, 
  348.                        UErrorCode& error) const;
  349.  
  350.     /**
  351.      * Returns the decimal format symbols, which is generally not changed
  352.      * by the programmer or user.
  353.      * @return desired DecimalFormatSymbols
  354.      * @see DecimalFormatSymbols
  355.      */
  356.     virtual const DecimalFormatSymbols* getDecimalFormatSymbols(void) const;
  357.  
  358.     /**
  359.      * Sets the decimal format symbols, which is generally not changed
  360.      * by the programmer or user.
  361.      * @param symbolsToAdopt DecimalFormatSymbols to be adopted.
  362.      */
  363.     virtual void adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt);
  364.  
  365.     /**
  366.      * Sets the decimal format symbols, which is generally not changed
  367.      * by the programmer or user.
  368.      * @param symbols DecimalFormatSymbols.
  369.      */
  370.     virtual void setDecimalFormatSymbols(const DecimalFormatSymbols& symbols);
  371.  
  372.  
  373.     /**
  374.      * Get the positive prefix.
  375.      *
  376.      * Examples: +123, $123, sFr123
  377.      */
  378.     UnicodeString& getPositivePrefix(UnicodeString& result) const;
  379.  
  380.     /**
  381.      * Set the positive prefix.
  382.      *
  383.      * Examples: +123, $123, sFr123
  384.      */
  385.     virtual void setPositivePrefix(const UnicodeString& newValue);
  386.  
  387.     /**
  388.      * Get the negative prefix.
  389.      *
  390.      * Examples: -123, ($123) (with negative suffix), sFr-123
  391.      */
  392.     UnicodeString& getNegativePrefix(UnicodeString& result) const;
  393.  
  394.     /**
  395.      * Set the negative prefix.
  396.      *
  397.      * Examples: -123, ($123) (with negative suffix), sFr-123
  398.      */
  399.     virtual void setNegativePrefix(const UnicodeString& newValue);
  400.  
  401.     /**
  402.      * Get the positive suffix.
  403.      *
  404.      * Example: 123%
  405.      */
  406.     UnicodeString& getPositiveSuffix(UnicodeString& result) const;
  407.  
  408.     /**
  409.      * Set the positive suffix.
  410.      *
  411.      * Example: 123%
  412.      */
  413.     virtual void setPositiveSuffix(const UnicodeString& newValue);
  414.  
  415.     /**
  416.      * Get the negative suffix.
  417.      *
  418.      * Examples: -123%, ($123) (with positive suffixes)
  419.      */
  420.     UnicodeString& getNegativeSuffix(UnicodeString& result) const;
  421.  
  422.     /**
  423.      * Set the positive suffix.
  424.      *
  425.      * Examples: 123%
  426.      */
  427.     virtual void setNegativeSuffix(const UnicodeString& newValue);
  428.  
  429.     /**
  430.      * Get the multiplier for use in percent, permill, etc.
  431.      * For a percentage, set the suffixes to have "%" and the multiplier to be 100.
  432.      * (For Arabic, use arabic percent symbol).
  433.      * For a permill, set the suffixes to have "\u2031" and the multiplier to be 1000.
  434.      *
  435.      * Examples: with 100, 1.23 -> "123", and "123" -> 1.23
  436.      */
  437.     int32_t getMultiplier(void) const;
  438.  
  439.     /**
  440.      * Set the multiplier for use in percent, permill, etc.
  441.      * For a percentage, set the suffixes to have "%" and the multiplier to be 100.
  442.      * (For Arabic, use arabic percent symbol).
  443.      * For a permill, set the suffixes to have "\u2031" and the multiplier to be 1000.
  444.      *
  445.      * Examples: with 100, 1.23 -> "123", and "123" -> 1.23
  446.      */
  447.     virtual void setMultiplier(int32_t newValue);
  448.  
  449.     /**
  450.      * Get the rounding increment.
  451.      * @return A positive rounding increment, or 0.0 if rounding
  452.      * is not in effect.
  453.      * @see #setRoundingIncrement
  454.      * @see #getRoundingMode
  455.      * @see #setRoundingMode
  456.      */
  457.     virtual double getRoundingIncrement(void);
  458.  
  459.     /**
  460.      * Set the rounding increment.  This method also controls whether
  461.      * rounding is enabled.
  462.      * @param newValue A positive rounding increment, or 0.0 to disable rounding.
  463.      * Negative increments are equivalent to 0.0.
  464.      * @see #getRoundingIncrement
  465.      * @see #getRoundingMode
  466.      * @see #setRoundingMode
  467.      */
  468.     virtual void setRoundingIncrement(double newValue);
  469.  
  470.     /**
  471.      * Get the rounding mode.
  472.      * @return A rounding mode
  473.      * @see #setRoundingIncrement
  474.      * @see #getRoundingIncrement
  475.      * @see #setRoundingMode
  476.      */
  477.     virtual ERoundingMode getRoundingMode(void);
  478.  
  479.     /**
  480.      * Set the rounding mode.  This has no effect unless the rounding
  481.      * increment is greater than zero.
  482.      * @param roundingMode A rounding mode
  483.      * @see #setRoundingIncrement
  484.      * @see #getRoundingIncrement
  485.      * @see #getRoundingMode
  486.      */
  487.     virtual void setRoundingMode(ERoundingMode roundingMode);
  488.  
  489.     /**
  490.      * Get the width to which the output of <code>format()</code> is padded.
  491.      * @return the format width, or zero if no padding is in effect
  492.      * @see #setFormatWidth
  493.      * @see #getPadCharacter
  494.      * @see #setPadCharacter
  495.      * @see #getPadPosition
  496.      * @see #setPadPosition
  497.      */
  498.     virtual int32_t getFormatWidth(void);
  499.  
  500.     /**
  501.      * Set the width to which the output of <code>format()</code> is padded.
  502.      * This method also controls whether padding is enabled.
  503.      * @param width the width to which to pad the result of
  504.      * <code>format()</code>, or zero to disable padding.  A negative
  505.      * width is equivalent to 0.
  506.      * @see #getFormatWidth
  507.      * @see #getPadCharacter
  508.      * @see #setPadCharacter
  509.      * @see #getPadPosition
  510.      * @see #setPadPosition
  511.      */
  512.     virtual void setFormatWidth(int32_t width);
  513.  
  514.     /**
  515.      * Get the character used to pad to the format width.  The default is ' '.
  516.      * @return the pad character
  517.      * @see #setFormatWidth
  518.      * @see #getFormatWidth
  519.      * @see #setPadCharacter
  520.      * @see #getPadPosition
  521.      * @see #setPadPosition
  522.      */
  523.     virtual UChar getPadCharacter(void);
  524.  
  525.     /**
  526.      * Set the character used to pad to the format width.  This has no effect
  527.      * unless padding is enabled.
  528.      * @param padChar the pad character
  529.      * @see #setFormatWidth
  530.      * @see #getFormatWidth
  531.      * @see #getPadCharacter
  532.      * @see #getPadPosition
  533.      * @see #setPadPosition
  534.      */
  535.     virtual void setPadCharacter(UChar padChar);
  536.  
  537.     /**
  538.      * Get the position at which padding will take place.  This is the location
  539.      * at which padding will be inserted if the result of <code>format()</code>
  540.      * is shorter than the format width.
  541.      * @return the pad position, one of <code>kPadBeforePrefix</code>,
  542.      * <code>kPadAfterPrefix</code>, <code>kPadBeforeSuffix</code>, or
  543.      * <code>kPadAfterSuffix</code>.
  544.      * @see #setFormatWidth
  545.      * @see #getFormatWidth
  546.      * @see #setPadCharacter
  547.      * @see #getPadCharacter
  548.      * @see #setPadPosition
  549.      * @see #kPadBeforePrefix
  550.      * @see #kPadAfterPrefix
  551.      * @see #kPadBeforeSuffix
  552.      * @see #kPadAfterSuffix
  553.      */
  554.     virtual EPadPosition getPadPosition(void);
  555.  
  556.     /**
  557.      * <strong><font face=helvetica color=red>NEW</font></strong>
  558.      * Set the position at which padding will take place.  This is the location
  559.      * at which padding will be inserted if the result of <code>format()</code>
  560.      * is shorter than the format width.  This has no effect unless padding is
  561.      * enabled.
  562.      * @param padPos the pad position, one of <code>kPadBeforePrefix</code>,
  563.      * <code>kPadAfterPrefix</code>, <code>kPadBeforeSuffix</code>, or
  564.      * <code>kPadAfterSuffix</code>.
  565.      * @see #setFormatWidth
  566.      * @see #getFormatWidth
  567.      * @see #setPadCharacter
  568.      * @see #getPadCharacter
  569.      * @see #getPadPosition
  570.      * @see #kPadBeforePrefix
  571.      * @see #kPadAfterPrefix
  572.      * @see #kPadBeforeSuffix
  573.      * @see #kPadAfterSuffix
  574.      */
  575.     virtual void setPadPosition(EPadPosition padPos);
  576.  
  577.     /**
  578.      * Return whether or not scientific notation is used.
  579.      * @return TRUE if this object formats and parses scientific notation
  580.      * @see #setScientificNotation
  581.      * @see #getMinimumExponentDigits
  582.      * @see #setMinimumExponentDigits
  583.      * @see #isExponentSignAlwaysShown
  584.      * @see #setExponentSignAlwaysShown
  585.      */
  586.     virtual bool_t isScientificNotation(void);
  587.  
  588.     /**
  589.      * Set whether or not scientific notation is used.
  590.      * @param useScientific TRUE if this object formats and parses scientific
  591.      * notation
  592.      * @see #isScientificNotation
  593.      * @see #getMinimumExponentDigits
  594.      * @see #setMinimumExponentDigits
  595.      * @see #isExponentSignAlwaysShown
  596.      * @see #setExponentSignAlwaysShown
  597.      */
  598.     virtual void setScientificNotation(bool_t useScientific);
  599.  
  600.     /**
  601.      * Return the minimum exponent digits that will be shown.
  602.      * @return the minimum exponent digits that will be shown
  603.      * @see #setScientificNotation
  604.      * @see #isScientificNotation
  605.      * @see #setMinimumExponentDigits
  606.      * @see #isExponentSignAlwaysShown
  607.      * @see #setExponentSignAlwaysShown
  608.      */
  609.     virtual int8_t getMinimumExponentDigits(void);
  610.  
  611.     /**
  612.      * Set the minimum exponent digits that will be shown.  This has no
  613.      * effect unless scientific notation is in use.
  614.      * @param minExpDig a value >= 1 indicating the fewest exponent digits
  615.      * that will be shown.  Values less than 1 will be treated as 1.
  616.      * @see #setScientificNotation
  617.      * @see #isScientificNotation
  618.      * @see #getMinimumExponentDigits
  619.      * @see #isExponentSignAlwaysShown
  620.      * @see #setExponentSignAlwaysShown
  621.      */
  622.     virtual void setMinimumExponentDigits(int8_t minExpDig);
  623.  
  624.     /**
  625.      * Return whether the exponent sign is always shown.
  626.      * @return TRUE if the exponent is always prefixed with either the
  627.      * localized minus sign or the localized plus sign, false if only negative
  628.      * exponents are prefixed with the localized minus sign.
  629.      * @see #setScientificNotation
  630.      * @see #isScientificNotation
  631.      * @see #setMinimumExponentDigits
  632.      * @see #getMinimumExponentDigits
  633.      * @see #setExponentSignAlwaysShown
  634.      */
  635.     virtual bool_t isExponentSignAlwaysShown(void);
  636.  
  637.     /**
  638.      * Set whether the exponent sign is always shown.  This has no effect
  639.      * unless scientific notation is in use.
  640.      * @param expSignAlways TRUE if the exponent is always prefixed with either
  641.      * the localized minus sign or the localized plus sign, false if only
  642.      * negative exponents are prefixed with the localized minus sign.
  643.      * @see #setScientificNotation
  644.      * @see #isScientificNotation
  645.      * @see #setMinimumExponentDigits
  646.      * @see #getMinimumExponentDigits
  647.      * @see #isExponentSignAlwaysShown
  648.      */
  649.     virtual void setExponentSignAlwaysShown(bool_t expSignAlways);
  650.  
  651.     /**
  652.      * Return the grouping size. Grouping size is the number of digits between
  653.      * grouping separators in the integer portion of a number.  For example,
  654.      * in the number "123,456.78", the grouping size is 3.
  655.      * @see setGroupingSize
  656.      * @see NumberFormat::isGroupingUsed
  657.      * @see DecimalFormatSymbols::getGroupingSeparator
  658.      */
  659.     int32_t getGroupingSize(void) const;
  660.  
  661.     /**
  662.      * Set the grouping size. Grouping size is the number of digits between
  663.      * grouping separators in the integer portion of a number.  For example,
  664.      * in the number "123,456.78", the grouping size is 3.
  665.      * @see getGroupingSize
  666.      * @see NumberFormat::setGroupingUsed
  667.      * @see DecimalFormatSymbols::setGroupingSeparator
  668.      */
  669.     virtual void setGroupingSize(int32_t newValue);
  670.  
  671.     /**
  672.      * Allows you to get the behavior of the decimal separator with integers.
  673.      * (The decimal separator will always appear with decimals.)
  674.      *
  675.      * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
  676.      */
  677.     bool_t isDecimalSeparatorAlwaysShown(void) const;
  678.  
  679.     /**
  680.      * Allows you to set the behavior of the decimal separator with integers.
  681.      * (The decimal separator will always appear with decimals.)
  682.      *
  683.      * Example: Decimal ON: 12345 -> 12345.; OFF: 12345 -> 12345
  684.      */
  685.     virtual void setDecimalSeparatorAlwaysShown(bool_t newValue);
  686.  
  687.     /**
  688.      * Synthesizes a pattern string that represents the current state
  689.      * of this Format object.
  690.      * @see applyPattern
  691.      */
  692.     virtual UnicodeString& toPattern(UnicodeString& result) const;
  693.  
  694.     /**
  695.      * Synthesizes a localized pattern string that represents the current
  696.      * state of this Format object.
  697.      *
  698.      * @see applyPattern
  699.      */
  700.     virtual UnicodeString& toLocalizedPattern(UnicodeString& result) const;
  701.  
  702.     /**
  703.      * Apply the given pattern to this Format object.  A pattern is a
  704.      * short-hand specification for the various formatting properties.
  705.      * These properties can also be changed individually through the
  706.      * various setter methods.
  707.      * <P>
  708.      * There is no limit to integer digits are set
  709.      * by this routine, since that is the typical end-user desire;
  710.      * use setMaximumInteger if you want to set a real value.
  711.      * For negative numbers, use a second pattern, separated by a semicolon
  712.      * <pre>
  713.      * .      Example "#,#00.0#" -> 1,234.56
  714.      * </pre>
  715.      * This means a minimum of 2 integer digits, 1 fraction digit, and
  716.      * a maximum of 2 fraction digits.
  717.      * <pre>
  718.      * .      Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
  719.      * </pre>
  720.      * In negative patterns, the minimum and maximum counts are ignored;
  721.      * these are presumed to be set in the positive pattern.
  722.      *
  723.      * @param pattern   The pattern to be applied.
  724.      * @param status    Output param set to success/failure code on
  725.      *                  exit. If the pattern is invalid, this will be
  726.      *                  set to a failure result.
  727.      */
  728.     virtual void applyPattern(const UnicodeString& pattern,
  729.                               UErrorCode& status);
  730.  
  731.     /**
  732.      * Apply the given pattern to this Format object.  The pattern
  733.      * is assumed to be in a localized notation. A pattern is a
  734.      * short-hand specification for the various formatting properties.
  735.      * These properties can also be changed individually through the
  736.      * various setter methods.
  737.      * <P>
  738.      * There is no limit to integer digits are set
  739.      * by this routine, since that is the typical end-user desire;
  740.      * use setMaximumInteger if you want to set a real value.
  741.      * For negative numbers, use a second pattern, separated by a semicolon
  742.      * <pre>
  743.      * .      Example "#,#00.0#" -> 1,234.56
  744.      * </pre>
  745.      * This means a minimum of 2 integer digits, 1 fraction digit, and
  746.      * a maximum of 2 fraction digits.
  747.      *
  748.      * Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
  749.      *
  750.      * In negative patterns, the minimum and maximum counts are ignored;
  751.      * these are presumed to be set in the positive pattern.
  752.      *
  753.      * @param pattern   The localized pattern to be applied.
  754.      * @param status    Output param set to success/failure code on
  755.      *                  exit. If the pattern is invalid, this will be
  756.      *                  set to a failure result.
  757.      */
  758.     virtual void applyLocalizedPattern(const UnicodeString& pattern,
  759.                                        UErrorCode& status);
  760.  
  761.     /**
  762.      * Sets the maximum number of digits allowed in the integer portion of a
  763.      * number. This override limits the integer digit count to 309.
  764.      * @see NumberFormat#setMaximumIntegerDigits
  765.      */
  766.     virtual void setMaximumIntegerDigits(int32_t newValue);
  767.  
  768.     /**
  769.      * Sets the minimum number of digits allowed in the integer portion of a
  770.      * number. This override limits the integer digit count to 309.
  771.      * @see NumberFormat#setMinimumIntegerDigits
  772.      */
  773.     virtual void setMinimumIntegerDigits(int32_t newValue);
  774.  
  775.     /**
  776.      * Sets the maximum number of digits allowed in the fraction portion of a
  777.      * number. This override limits the fraction digit count to 340.
  778.      * @see NumberFormat#setMaximumFractionDigits
  779.      */
  780.     virtual void setMaximumFractionDigits(int32_t newValue);
  781.  
  782.     /**
  783.      * Sets the minimum number of digits allowed in the fraction portion of a
  784.      * number. This override limits the fraction digit count to 340.
  785.      * @see NumberFormat#setMinimumFractionDigits
  786.      */
  787.     virtual void setMinimumFractionDigits(int32_t newValue);
  788.  
  789.     /**
  790.      * The resource tags we use to retrieve decimal format data from
  791.      * locale resource bundles.
  792.      */
  793.     static const UnicodeString fgNumberPatterns;
  794.  
  795. public:
  796.  
  797.     /**
  798.      * Return the class ID for this class.  This is useful only for
  799.      * comparing to a return value from getDynamicClassID().  For example:
  800.      * <pre>
  801.      * .      Base* polymorphic_pointer = createPolymorphicObject();
  802.      * .      if (polymorphic_pointer->getDynamicClassID() ==
  803.      * .          Derived::getStaticClassID()) ...
  804.      * </pre>
  805.      * @return          The class ID for all objects of this class.
  806.      */
  807.     static UClassID getStaticClassID(void) { return (UClassID)&fgClassID; }
  808.  
  809.     /**
  810.      * Returns a unique class ID POLYMORPHICALLY.  Pure virtual override.
  811.      * This method is to implement a simple version of RTTI, since not all
  812.      * C++ compilers support genuine RTTI.  Polymorphic operator==() and
  813.      * clone() methods call this method.
  814.      *
  815.      * @return          The class ID for this object. All objects of a
  816.      *                  given class have the same class ID.  Objects of
  817.      *                  other classes have different class IDs.
  818.      */
  819.     virtual UClassID getDynamicClassID(void) const { return getStaticClassID(); }
  820.  
  821. private:
  822.     static char fgClassID;
  823.  
  824.     /**
  825.      * Do real work of constructing a new DecimalFormat.
  826.      */
  827.     void construct(UErrorCode&               status,
  828.                    const UnicodeString*     pattern = 0,
  829.                    DecimalFormatSymbols*    symbolsToAdopt = 0,
  830.                    const Locale&            locale = Locale::getDefault());
  831.  
  832.     /**
  833.      * Does the real work of generating a pattern.
  834.      */
  835.     UnicodeString& toPattern(UnicodeString& result, bool_t localized) const;
  836.  
  837.     /**
  838.      * Does the real work of applying a pattern.
  839.      * @param pattern   The pattern to be applied.
  840.      * @param localized If true, the pattern is localized; else false.
  841.      * @param status    Output param set to success/failure code on
  842.      *                  exit. If the pattern is invalid, this will be
  843.      *                  set to a failure result.
  844.      */
  845.     void applyPattern(const UnicodeString& pattern,
  846.                             bool_t localized,
  847.                             UErrorCode& status);
  848.  
  849.     /**
  850.      * Do the work of formatting a number, either a double or a long.
  851.      */
  852.     UnicodeString& subformat(UnicodeString& result,
  853.                              FieldPosition& fieldPosition,
  854.                              bool_t         isNegative,
  855.                              bool_t         isInteger) const;
  856.  
  857.     static const int32_t fgStatusInfinite;
  858.     static const int32_t fgStatusPositive;
  859.     static const int32_t fgStatusLength;
  860.  
  861.     /**
  862.      * Parse the given text into a number.  The text is parsed beginning at
  863.      * parsePosition, until an unparseable character is seen.
  864.      * @param text The string to parse.
  865.      * @param parsePosition The position at which to being parsing.  Upon
  866.      * return, the first unparseable character.
  867.      * @param digits The DigitList to set to the parsed value.
  868.      * @param isExponent If true, parse an exponent.  This means no
  869.      * infinite values and integer only.
  870.      * @param status Upon return contains boolean status flags indicating
  871.      * whether the value was infinite and whether it was positive.
  872.      */
  873.     bool_t subparse(const UnicodeString& text, ParsePosition& parsePosition,
  874.                     DigitList& digits, bool_t isExponent,
  875.                     bool_t* status) const;
  876.  
  877.     /**
  878.      * Append an affix to the given StringBuffer, using quotes if
  879.      * there are special characters.  Single quotes themselves must be
  880.      * escaped in either case.
  881.      */
  882.     void appendAffix(UnicodeString& buffer, const UnicodeString& affix, 
  883.                      bool_t localized) const;
  884.  
  885.     void appendAffix(UnicodeString& buffer,
  886.                      const UnicodeString* affixPattern,
  887.                      const UnicodeString& expAffix, bool_t localized) const;
  888.  
  889.     void expandAffix(const UnicodeString& pattern,
  890.                      UnicodeString& affix) const;
  891.  
  892.     void expandAffixes();
  893.     
  894.     static double round(double a, ERoundingMode mode, bool_t isNegative);
  895.  
  896.     void addPadding(UnicodeString& result, bool_t hasAffixes,
  897.                     bool_t isNegative) const;
  898.  
  899.     /**
  900.      * Constants.
  901.      */
  902.     static const int8_t fgMaxDigit; // The largest digit, in this case 9
  903.  
  904.     /*transient*/ DigitList* fDigitList;
  905.  
  906.     UnicodeString           fPositivePrefix;
  907.     UnicodeString           fPositiveSuffix;
  908.     UnicodeString           fNegativePrefix;
  909.     UnicodeString           fNegativeSuffix;
  910.     UnicodeString*          fPosPrefixPattern;
  911.     UnicodeString*          fPosSuffixPattern;
  912.     UnicodeString*          fNegPrefixPattern;
  913.     UnicodeString*          fNegSuffixPattern;
  914.     int32_t                 fMultiplier;
  915.     int32_t                 fGroupingSize;
  916.     bool_t                  fDecimalSeparatorAlwaysShown;
  917.     /*transient*/ bool_t    fIsCurrencyFormat;
  918.     DecimalFormatSymbols*   fSymbols;
  919.  
  920.     bool_t                  fUseExponentialNotation;
  921.     int8_t                  fMinExponentDigits;
  922.     bool_t                  fExponentSignAlwaysShown;
  923.  
  924.     /* If fRoundingIncrement is NULL, there is no rounding.  Otherwise, round to
  925.      * fRoundingIncrement.getDouble().  Since this operation may be expensive,
  926.      * we cache the result in fRoundingDouble.  All methods that update
  927.      * fRoundingIncrement also update fRoundingDouble. */
  928.     DigitList*              fRoundingIncrement;
  929.     /*transient*/ double    fRoundingDouble;
  930.     ERoundingMode           fRoundingMode;
  931.  
  932.     UChar                   fPad;
  933.     int32_t                 fFormatWidth;
  934.     EPadPosition            fPadPosition;
  935.  
  936.     // Constants for characters used in programmatic (unlocalized) patterns.
  937.     static const UChar    kPatternZeroDigit;
  938.     static const UChar    kPatternGroupingSeparator;
  939.     static const UChar    kPatternDecimalSeparator;
  940.     static const UChar    kPatternPerMill;
  941.     static const UChar    kPatternPercent;
  942.     static const UChar    kPatternDigit;
  943.     static const UChar    kPatternSeparator;
  944.     static const UChar    kPatternExponent;
  945.     static const UChar    kPatternPlus;
  946.     static const UChar    kPatternMinus;
  947.     static const UChar    kPatternPadEscape;
  948.  
  949.     /**
  950.      * The CURRENCY_SIGN is the standard Unicode symbol for currency.  It
  951.      * is used in patterns and substitued with either the currency symbol,
  952.      * or if it is doubled, with the international currency symbol.  If the
  953.      * CURRENCY_SIGN is seen in a pattern, then the decimal separator is
  954.      * replaced with the monetary decimal separator.
  955.      */
  956.     static const UChar    kCurrencySign;
  957.     static const UChar    kQuote;
  958.  
  959. protected:
  960.     static const int32_t  kDoubleIntegerDigits;
  961.     static const int32_t  kDoubleFractionDigits;
  962. };
  963.  
  964. inline UnicodeString&
  965. DecimalFormat::format(const Formattable& obj,
  966.                       UnicodeString& result,
  967.                       UErrorCode& status) const {
  968.     // Don't use Format:: - use immediate base class only,
  969.     // in case immediate base modifies behavior later.
  970.     return NumberFormat::format(obj, result, status);
  971. }
  972.  
  973. inline UnicodeString&
  974. DecimalFormat::format(double number,
  975.                       UnicodeString& output) const {
  976.     return NumberFormat::format(number, output);
  977. }
  978.  
  979. inline UnicodeString&
  980. DecimalFormat::format(int32_t number,
  981.                       UnicodeString& output) const {
  982.     return NumberFormat::format(number, output);
  983. }
  984.  
  985. #endif // _DECIMFMT
  986. //eof
  987.